﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Services;

namespace Workflow.Application.Step
{
    /// <summary>
    /// 流程驳回进行
    /// </summary>
    public class RejectStep : StepBodyAsync
    {
        private readonly IWorkflowRegistry workflowRegistry;

        public RejectStep(IWorkflowRegistry workflowRegistry)
        {
            this.workflowRegistry = workflowRegistry;
        }

        public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
        {
            try
            {
                var def = workflowRegistry.GetDefinition(context.Workflow.WorkflowDefinitionId, context.Workflow.Version);
                // 4.2 设置驳回路由
                context.Step.Outcomes.Clear();
                context.Step.Outcomes.Add(new ValueOutcome()
                {
                    ExternalNextStepId = nameof(EndStep),
                    NextStep = def.Steps.FirstOrDefault(p => p.ExternalId == nameof(EndStep))!.Id
                });
                return ExecutionResult.Next();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
